<?php

// [ 微信支付 ]

namespace app\wechat\controller;
use think\Controller;
use app\common\lib\Order;

class Pay extends Controller
{
    // 微信支付异步回调
    // http://jzl.51dojob.com/wechat/pay/callback
    public function callback()
    {
        $postXml = $GLOBALS["HTTP_RAW_POST_DATA"];
        if ( !empty($postXml) ) {
            $postObj = simplexml_load_string($postXml, 'SimpleXMLElement', LIBXML_NOCDATA);
            $postArr = json_decode(json_encode($postObj),true);

            $returnCode = $postArr['return_code'];
            if ('SUCCESS' == $returnCode) {
                $resultCode = $postArr['result_code'];
                if ('SUCCESS' == $resultCode) {
                    $out_trade_no = $postArr['out_trade_no'];
                    $bool = Order::callbackHandle($out_trade_no);
                }
            }
        }

        $msg = '<xml>
                  <return_code><![CDATA[SUCCESS]]></return_code>
                  <return_msg><![CDATA[OK]]></return_msg>
                </xml>';
        return $msg;
    }
}